
namespace HeaderInserter
{
    using DevExpress.CodeRush.Core;
    using DevExpress.CodeRush.PlugInCore;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public partial class Inserter : StandardPlugIn
    {
        /// <summary>
        /// Inserts a new page header into the file.
        /// </summary>
        /// <param name="ea">The <see cref="DevExpress.CodeRush.Core.ExecuteEventArgs"/> instance containing the event data.</param>
        private void Insert_Execute(ExecuteEventArgs ea)
        {
            if (CodeRush.Documents.ActiveTextView == null)
            {
                return;
            }
            var marker = CodeRush.Markers.Drop();
            var fileName = CodeRush.Documents.ActiveTextDocument.FullName.Split('\\').Last();
            var copyright = Insert.Parameters["Copyright"].ValueAsStr.ButIfThisIsNullOrEmptyThenUse(Insert.Parameters["Copyright"].DefaultValue.ToString());
            var companyName = Insert.Parameters["Company Name"].ValueAsStr.ButIfThisIsNullOrEmptyThenUse(Insert.Parameters["Company Name"].DefaultValue.ToString());
            var header = new List<string>() {
                @"// ----------------------------------------------------------------",
                string.Format("// <copyright file=\"{0}\" company=\"{1}\">", fileName, companyName),
                string.Format("// {0}", copyright),
                @"// </copyright>",
                string.Format("// <author>{0}</author>", Environment.UserName),
                string.Format("// <created>{0}</created>", DateTime.Now.ToLongDateString()),
                @"// -----------------------------------------------------------------",
                string.Empty
            };

            CodeRush.Documents.ActiveTextDocument.InsertLines(1, header.ToArray());
            var headerLength = header.Count;
            AlertActionComplete.PointTo(headerLength, header.Max(line => line.Length));
            CodeRush.Documents.ActiveTextDocument.ActiveView.Caret.MoveTo(marker.Line, marker.Offset);
            CodeRush.Markers.Remove(marker);
        }

        public override void FinalizePlugIn()
        {
            base.FinalizePlugIn();
        }
        public override void InitializePlugIn()
        {
            base.InitializePlugIn();
        }
    }
}
